package com.anjubao.parking.municipal.parkinglot.controller;

import com.anjubao.parking.municipal.common.dto.ApiCode;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.*;
import com.anjubao.parking.municipal.parkinglot.feign.ParkingDeviceFeignService;
import com.anjubao.parking.municipal.parkinglot.form.VideoPileQueryForm;
import com.anjubao.parking.municipal.parkinglot.service.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 设备故障表controller
 *
 * @author anjubao
 */
@RestController
@Api(value = "FeignController", tags = {"内部调用接口"})
@RequestMapping("/feign")
public class FeignController implements ParkingDeviceFeignService {

    @Autowired
    private ParkingLotDeviceFailureService parkingLotDeviceFailureService;
    @Autowired
    private ParkingGeomagneticService parkingGeomagneticService;
    @Resource
    private ParkingLotDeviceChargingPileService parkingLotDeviceChargingPileService;
    @Autowired
    private ParkingLotDeviceVideoPileSlotService parkingLotDeviceVideoPileSlotService;
    @Autowired
    private ParkingLotDeviceVideoPileService parkingLotDeviceVideoPileService;
    @Autowired
    private ParkingLotDeviceLogService parkingLotDeviceLogService;
    @Autowired
    private ParkingLotDeviceLogPictureService parkingLotDeviceLogPictureService;
    private static final Logger logger = LoggerFactory.getLogger(FeignController.class);

    /**
     * 根据泊位code获取设备状态
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "根据泊位code获取设备状态", notes = "统计设备状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "泊位编号", required = true, dataType = "String"),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @GetMapping("/parking_lot_device_failure/{code}")
    @Override
    public ApiResult<List<ParkingLotSlotFailureDto>> selectDevices(@PathVariable String code) {
        logger.info("统计设备状态 {} ", code);
        List<ParkingLotSlotFailureDto> parkingLotSlotFailureDtoList = parkingLotDeviceFailureService.getStatusFromFailure(code);
        return ApiResult.success(parkingLotSlotFailureDtoList);
    }

    /**
     * 根据车场code获取安装了地磁的泊位id列表
     *
     * @param parkingLotCode
     * @return
     */
    @ApiOperation(value = "根据车场code获取地磁对应的泊位列表", notes = "统计地磁泊位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parkingLotCode", value = "车场编号", required = true, dataType = "String"),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @GetMapping("/parking_lot_device_geomagnetic/{parkingLotCode}")
    @Override
    public ApiResult<List<Long>> selectGeomagnetic(@PathVariable String parkingLotCode) {
        logger.info("统计地磁泊位列表 {} ", parkingLotCode);
        List<Long> parkingLotSlots = parkingGeomagneticService.selectGeomagnetic(parkingLotCode);
        return ApiResult.success(parkingLotSlots);
    }

    /**
     * 根据车场code获取安装了充电桩的泊位id列表
     *
     * @param parkingLotCode
     * @return
     */
    @ApiOperation(value = "根据车场code获取充电桩对应的泊位列表", notes = "统计充电桩泊位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parkingLotCode", value = "车场编号", required = true, dataType = "String"),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @GetMapping("/parking_lot_device_charging/{parkingLotCode}")
    @Override
    public ApiResult<List<Long>> selectCharging(@PathVariable String parkingLotCode) {
        logger.info("统计充电桩泊位列表 {} ", parkingLotCode);
        List<Long> parkingLotSlots = parkingLotDeviceChargingPileService.selectCharging(parkingLotCode);
        return ApiResult.success(parkingLotSlots);
    }

    /**
     * 根据车场code获取安装了视频桩的泊位id列表
     *
     * @param parkingLotCode
     * @return
     */
    @ApiOperation(value = "根据车场code获取视频桩对应的泊位列表", notes = "统计视频桩泊位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parkingLotCode", value = "车场编号", required = true, dataType = "String"),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @GetMapping("/parking_lot_device_video/{parkingLotCode}")
    @Override
    public ApiResult<List<Long>> selectVideo(@PathVariable String parkingLotCode) {
        logger.info("统计视频桩泊位列表 {} ", parkingLotCode);
        List<Long> parkingLotSlots = parkingLotDeviceVideoPileSlotService.selectVideo(parkingLotCode);
        return ApiResult.success(parkingLotSlots);
    }

    /**
     * 查询视频桩信息接口
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "查询视频桩信息接口")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    @GetMapping("/parking-lots-device-videopiles")
    @Override
    public ApiResult<List<VideoPileDto>> getVideoPile(@RequestBody @Valid @ApiParam(value = "视频桩信息") VideoPileQueryForm form) {
        if (form.getCityId() == null && form.getDistrictId() == null && form.getProvinceId() == null) {
            return new ApiResult<>(ApiCode.STATUS_INTERNAL_ERROR.getCode(), "省市区ID不允许同时为空");
        }
        logger.info("查询视频桩信息 {}", form);
        Page<VideoPileDto> page = new Page<>();
        page.setSize(form.getPageSize());
        page.setCurrent(form.getPage());
        Page<VideoPileDto> videoPileLists = parkingLotDeviceVideoPileService.getVideoPiles(page, form);
        return ApiResult.success(videoPileLists.getRecords(), (int) videoPileLists.getTotal());
    }

    /**
     * 新增log信息接口
     *
     * @param deviceLogDtoList
     * @return
     */
    @ApiOperation(value = "新增log信息接口")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    @PostMapping("/parking-lot-device-log")
    @Override
    public ApiResult saveLog(@RequestBody @Valid @ApiParam(value = "log信息") List<DeviceLogDto> deviceLogDtoList) {
        logger.info("新增log信息 {}", deviceLogDtoList);
        parkingLotDeviceLogService.saveLog(deviceLogDtoList);
        return ApiResult.success();
    }


    /**
     * 新增图片信息接口
     *
     * @param deviceLogPictureDtoList
     * @return
     */
    @ApiOperation(value = "新增图片信息接口")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    @PostMapping("/parking-lot-device-log-picture")
    @Override
    public ApiResult savePicture(@RequestBody @Valid @ApiParam(value = "图片信息") List<DeviceLogPictureDto> deviceLogPictureDtoList) {
        logger.info("新增图片信息 {}", deviceLogPictureDtoList);
        parkingLotDeviceLogPictureService.savePicture(deviceLogPictureDtoList);
        return ApiResult.success();
    }
}
